<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<style>
			.datepicker-input{height: 34px; border: 1px solid #D3D3D3; background-color: #fff; border-radius: 2px; width: 250px; padding: 0px 4px;}
		</style>
		<div class="super-theme-example">
			<div class="options-toolbar">
				<form id="orderdatagridQueryParams">
					<input class="easyui-textbox" name="code" data-options="width:120,prompt:'订单编号'">
					<input class="easyui-textbox" name="tel" data-options="width:120,prompt:'电话号码'">
					<input class="easyui-textbox" name="consignee" data-options="width:120,prompt:'收件人'">
					<input class="easyui-textbox" name="affiliate" data-options="width:120,prompt:'投放人'">
					
					<select id="paymentCombo" class="easyui-combobox" name="payment" style="width:150px;">
					    <option value="">全部方式</option>
					    <option value="1">货到付款</option>
					    <option value="2">stripe</option>
					</select>
					<select id="shopIdCombo" class="easyui-combobox" name="shopId" style="width:150px;"></select>
					
					<input id="order-datepicker" class="datepicker-input" />
					<a href="javascript:void(0)" class="easyui-linkbutton primary" data-options="iconCls:'fa fa-search'" onclick="app_order.methods.queryDatagrid();">查询</a>
				</form>
				<div class="top-options">
					<a href="javascript:void(0)" class="easyui-linkbutton info" data-options="iconCls:'fa fa-download'" onclick="exportExcel(commonConfig.shopUrlPrefix() + '/order/whole/export' , app_order.methods.initDatagridQueryParams())">导出</a>
				</div>
			</div>
			<table id="order-datagrid"></table>
			<div id="orderSkuDetais" class="easyui-window" title="订单商品详情" style="width:900px;height:440px" data-options="modal:true,closed:true,minimizable:false,maximizable:false">
				<table id="orderSkuDetais-datagrid" style="height: 400px;"></table>
			</div>
		</div>
		<script type="text/javascript">
			var app_order = {
				datas : {} , 
				methods : {
					initShopCombo: function(){
						$('#shopIdCombo').combobox({
						    url: commonConfig.shopUrlPrefix() + '/shop/combobox',
						    valueField: 'id',
						    textField: 'text'
						});
					},
					initDatagridQueryParams: function(){
						var params = $('#orderdatagridQueryParams').serializeArray();
						var queryParams = {};
						$.each(params , function(index , param){
						     queryParams[param.name] = param.value;
						})
						var time = $('#order-datepicker').val();
						queryParams.begainTime = timestamp(time.substring(0, 19));
						queryParams.endTime = timestamp(time.substring(22));
						
						queryParams.tel = queryParams.tel.replace(/[\r\n，]/g,",");
						queryParams.code = queryParams.code.replace(/[\r\n，]/g,",");
						queryParams.consignee = queryParams.consignee.replace(/[\r\n，]/g,",");
						
						return queryParams;
					},
					initTimeSelect: function(){
						$('#order-datepicker').daterangepicker($.extend(true, {} , globalDaterangepickerOptions));
					},
					initDatagrid : function(){
						
						$('#order-datagrid').datagrid({
							height: $(document).height() - 200,
						    url: commonConfig.shopUrlPrefix() + '/order/list',
						    queryParams: {
								begainTime: timestamp($('#order-datepicker').val().substring(0, 19)),
								endTime: timestamp($('#order-datepicker').val().substring(22))
							},
						    columns:[[
								{field:'code',title:'编码',width:100,align:'center',formatter: function(value,row,index){
									return '<a onclick="app_order.methods.showSKU('+index+');">'+value+'</a>';
								}},
								{field:'shopName',title:'商城',width:60,align:'center'},
								{field:'paymentName',title:'支付方式',width:80,align:'center'},
								{field:'statusName',title:'状态',width:60,align:'center'},
								{field:'price',title:'价格',width:100,align:'center',formatter: function(value,row,index){
									return '<span style="color:red;font-weight: bold;">'+row.price+'</span><small style="opacity: 0.8;color: blue;margin-left: 5px;">(运费:'+row.freight+')</small>';
								}},
                               {field:'promotions',title:'优惠',width:100,align:'center',formatter: function(value,row,index){
                               		if(value){
                               			var data = JSON.parse(value)
	                                    var html = ''
	                                    data.map(function(e){
	                                        html += '<span>' + e.promotionPrice + '( '+e.promotionName +' )' + '</span>'
	                                    })
	                                    return html;
                               		}
                               		return '--' ; 
                                }},
								{field:'affiliate',title:'投放人',width:60,align:'center',formatter: function(value,row,index){
									return value || '--';
								}},
								{field:'consignee',title:'收件人',width:150,align:'center',formatter: function(value,row,index){
									if(row.email){
										return value + '(' + row.tel + ')' + '<br/>' + '<span style="font-size:80%">' + row.email + '</span>'
									}
									return value + '(' + row.tel + ')';
								}},
								{field:'address',title:'配送地址',width:200,align:'center',formatter: function(value,row,index){
									return row.city + '-' + row.district + '-' + row.details;
								}},
								{field:'createdAt',title:'下单时间',width:120,align:'center',formatter: function(value,row,index){
									return FormatDate(value);
								}},
								{field:'remark',title:'用户留言',width:150,align:'center'},
								{field:'options',title:'操作',width:100,align:'center', formatter: function(value,row,index){
									if(row.status == 0){
										return '<div class="row-edit-group">' +
											        '<a onclick="app_order.methods.asyncToOrder(\''+ row.code +'\')">推送管理平台</a>' + 
									   		 		'<button class="row-btn primary" onclick="app_order.methods.deleteModule(\''+row.code+'\');">删除</button>'+
											   '</div>';
									}
									return '<button class="row-btn primary" onclick="app_order.methods.deleteModule(\''+row.code+'\');">删除</button>'; 
								}}
								
						    ]]
						});
					},
					deleteModule: function(code){
						$.messager.confirm('提示', '确定删除?', function(r){
							if (r){
								requestServer(commonConfig.shopUrlPrefix() + '/order/' + code, 'DELETE').then((response) => {
									if (response.code == 1) {
										showMessage('<i class="fa fa-info-circle"></i>&nbsp; 删除成功...');
										app_order.methods.queryDatagrid();
									}else {
										$.messager.alert('提示',response.message,'error');
									}
								}).catch((error) => {
									console.log(error)
								});
							}
						});
					},
					showSKU: function(index){
						$('#orderSkuDetais').window('open');
						$('#orderSkuDetais-datagrid').datagrid({
							data: $('#order-datagrid').datagrid('getRows')[index].orderSkuDTOs,
							pagination:false,
						    columns:[[
						    	{field:'productId',title:'产品ID',width:80,align:'center'},
								{field:'productSkuBarcode',title:'产品编码',width:100,align:'center'},
								{field:'productImageUrl',title:'产品图片',width:60,align:'center',formatter:function(value,row,index){return '<img class="td-img" src="'+ value +'">';}},
								{field:'productTitle',title:'产品名称',width:120,align:'center'},
								{field:'productSkuName',title:'产品规格',width:100,align:'center'},
								{field:'productPrice',title:'价格',width:80,align:'center'},
								{field:'quantity',title:'数量',width:80,align:'center'}
						    ]]
						});
						
					},
					queryDatagrid: function(){
						$('#order-datagrid').datagrid('load',app_order.methods.initDatagridQueryParams());
					},
					asyncToOrder: function(orderCode){
						requestBodyServer(commonConfig.shopUrlPrefix() + '/order/' + orderCode + '/async-admin', 'PUT').then((response) => {
							if (response.code == 1) {
								showMessage('<i class="fa fa-info-circle"></i>&nbsp; 修改成功...');
								$('#productEdit').window('close');
								app_order.methods.queryDatagrid();
							}else {
								$.messager.alert('提示',response.message,'error');
							}
						}).catch((error) => {
							console.log(error)
						});
					}
				},
				init : function(){
					app_order.methods.initShopCombo();
					app_order.methods.initTimeSelect();
					app_order.methods.initDatagrid();
				}
			}
			app_order.init();
		</script>
	</body>
</html>
